﻿using System.Web;
using Microsoft.SharePoint;

namespace DocLibraryReplicator.AppCode
{
    internal class ReplicatorLogger
    {
        public void writeToLog(string title, string logDate, string modifiedBy, string action, string copySource, string fileName, SPWeb web)
        {
            // Get the current httpcontext
            System.Web.HttpContext currentContext = System.Web.HttpContext.Current;
            // Set the httpcontext to null;
            HttpContext.Current = null;
            using (web)
            {
                try
                {
                    // Create an item the Replicator Logger list to record the event
                    SPList spList = web.Lists["Replicator Logger"];
                    SPListItem logItem = spList.Items.Add();
                    logItem["Title"] = title;
                    logItem["Log Date"] = logDate;
                    logItem["Modified By"] = modifiedBy;
                    logItem["Action"] = action;
                    logItem["Copy Source"] = copySource;
                    logItem["File Name"] = fileName;
                    logItem.Update();
                }

                catch (SPException spEx)
                {
                }

                finally
                {
                    // Set the httpcontext back to the original
                    System.Web.HttpContext.Current = currentContext;
                }
            }
        }
    }
}